package com.coder.oj.system.controller;

import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.core.JwtToken;
import com.coder.oj.common.core.R;
import com.coder.oj.common.mapper.CommitMapper;
import com.coder.oj.common.validate.ValidationGroups;
import com.coder.oj.system.dto.CommitDto;
import com.coder.oj.system.mapstruct.CommitMapstruct;
import com.coder.oj.system.query.CommitQuery;
import com.coder.oj.system.service.CommitService;
import com.coder.oj.system.vo.CommitStatisticsVo;
import com.coder.oj.system.vo.CommitVo;
import com.coder.oj.system.vo.PowerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = {"提交管理"})
@RestController
@RequestMapping("/commit")
public class CommitController {

    @Autowired
    private CommitMapper commitMapper;

    @Autowired
    private CommitMapstruct commitMapstruct;

    @Autowired
    private CommitService commitService;

    @JwtToken
    @PostMapping
    @ApiOperation("提交代码")
    public R<CommitVo> saveCommit(@Validated(ValidationGroups.Insert.class) @RequestBody CommitDto params) {
        return R.ok().setResult(commitService.runCode(params));
    }

    @PostMapping("/debug")
    @ApiOperation("代码调试")
    public R<String> debugCode(@Validated(ValidationGroups.Insert.class) @RequestBody CommitDto params) {
        return R.ok().setResult(commitService.debugCode(params));
    }

    @PostMapping("/page")
    @ApiOperation("分页查询")
    public R<JsonPage<CommitVo>> getCommitPage(@RequestBody CommitQuery params) {
        return R.ok().setResult(commitService.getCommitPage(params));
    }

    @JwtToken
    @GetMapping
    @ApiOperation("提交详情")
    public R<CommitVo> getCommitById(@RequestParam("id") Long id) {
        return R.ok().setResult(commitMapstruct.toVO(commitMapper.selectById(id)));
    }

    @GetMapping("/statistics")
    @ApiOperation("提交统计")
    public R<CommitStatisticsVo> statistics(@RequestParam("problemId") Long problemId) {
        return R.ok().setResult(commitService.statistics(problemId));
    }

    @GetMapping("/power")
    @ApiOperation("能力值")
    public R<List<PowerVo>> getPowerList(@RequestParam("userId") Long userId) {
        return R.ok().setResult(commitService.getPowerList(userId));
    }
}
